package org.example.game.weekly.game413;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/08/03 20:46
 */
public class Solution002 {

    public static void main(String[] args) {
        int[] res = new Solution002().resultsArray(new int[][]{{1,2},{3,4},{2,3},{-3,0}}, 2);
        for (int i = 0; i < res.length; i++) {
            System.out.println(res[i]);
        }

    }

    public int[] resultsArray(int[][] queries, int k) {
        int n = queries.length;
        int[] res = new int[n];
        if(k>=n){
            Arrays.fill(res, -1);
            return res;
        }

        if( k < 100){
            int[] dis = new int[k];
            Arrays.fill(dis, Integer.MAX_VALUE);
            for (int i = 0; i < n; i++) {
                int distance = Math.abs(queries[i][0]) + Math.abs(queries[i][1]);
                System.out.print(distance + " ");
                //维护前K个数组
                if(distance < dis[k-1]){
                    dis[k-1] = distance;
                    Arrays.sort(dis);
                }
                res[i] = dis[k-1] == Integer.MAX_VALUE ? -1 : dis[k-1];
            }
        }else {
            int[] dis = new int[n];
            for (int i = 0; i < n; i++) {
                int distance = Math.abs(queries[i][0]) + Math.abs(queries[i][1]);
                System.out.print(distance + " ");
                dis[i] = distance;
            }
            List<Integer> sortList = Arrays.stream(dis).boxed().sorted().collect(Collectors.toList());
            for (int i = n-1; i >=0; i--) {
                if(sortList.size() <= (k-1)){
                    res[i] = -1;
                }else {
                    res[i] = sortList.get(k-1);
                }
                sortList.remove(Integer.valueOf(dis[i]));
            }
        }
        return res;
    }

}
